/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.service.product;

import java.math.BigDecimal;

import com.bluemartini.dna.BMException;
import com.bluemartini.htmlapp.HTMLProductUtil;
import com.bluemartini.htmlapp.Sku;

import net.martinimix.dao.DataAccessException;
import net.martinimix.dao.product.ProductDao;
import net.martinimix.domain.product.Product;
import net.martinimix.domain.product.ProductNotFoundException;
import net.martinimix.domain.product.SkuNotFoundException;

/**
 * Provides a MartiniMix product service.
 * 
 * @author Scott Rossillo
 *
 */
public class MartiniMixProductService implements ProductService {

	/**
	 * The product data access object for this service.
	 */
	protected ProductDao productDao;
	
	/* (non-Javadoc)
	 * @see net.martinimix.service.product.ProductService#findProductById(java.lang.Long)
	 */
	public Product findProductById(Long id) throws ProductNotFoundException {
		
		Product product = productDao.findProductById(id);
		
		if(product == null) {
			throw new ProductNotFoundException("Cannot find product with id [" + id + "]");
		}
		
		return product;
	}

	/* (non-Javadoc)
	 * @see net.martinimix.service.product.ProductService#getProductPrice(java.lang.Long)
	 */
	public BigDecimal getProductPrice(Long productId) {
		
		return productDao.getProductPrice(productId);
	}

	public Sku findSku(Long id) throws SkuNotFoundException {
		
		Sku sku = null;
		
		try {
			sku = HTMLProductUtil.getSkuDetail(id);
			if(sku == null) {
				throw new SkuNotFoundException("Sku missing for [" + id + "]");
			}
		} catch (BMException e) {
			throw new DataAccessException("Unable to get sku details!", e);
		}
		
		return sku;
	}

	/**
	 * Sets the product data access object for this service.
	 * 
	 * @param productDao the <code>ProductDao</code> for this service
	 */
	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

}
